package menu;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;

public class Fader implements Runnable{
	private BufferedImage img1;
	private BufferedImage img2;
	private Graphics2D    g;
	
	public BufferedImage getImg1() {
		return img1;
	}

	public void setImg1(String imgPath) {
		this.img1 = LoadImg(imgPath);
	}

	public BufferedImage getImg2() {
		return img2;
	}

	public void setImg2(String imgPath) {
		this.img2 = LoadImg(imgPath);
	}

	public Graphics2D getGraphics() {
		return g;
	}

	public void setGraphics(Graphics2D g) {
		this.g = g;
	}

	public Fader()
	{
	}
	
	private BufferedImage LoadImg(String path)
	{
		BufferedImage img;
		URL url = MainGameScreen.class.getResource(path);
	    try
	    {
	    	System.out.println("FILE: " + url.getPath());
	    	img=ImageIO.read(url);
	    } 
	    catch(Exception e)
	    {
	    	System.out.println("Errore!!!");
	    	img=null;
	    }
	    return img;
	}
	
	public void xFade(long msec)
	{
		float fade = 0f;
		while (fade<1000)
		{
			fade=fade+(msec);
			g.drawImage(img1, 0, 0, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,fade/1000));
			g.drawImage(img2, 0, 0, null);
			try {
				Thread.sleep(msec);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void run() {
		xFade(5);
	}

}
